﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Layans.Data;
using LApi.Web.Models;

namespace LApi.Web.Controllers
{
    public class Default1Controller : ApiController
    {
        private LApiWebContext db = new LApiWebContext();

        // GET api/Default1
        public IQueryable<PageApi> GetPageApis()
        {
            return db.PageApis;
        }

        // GET api/Default1/5
        [ResponseType(typeof(PageApi))]
        public IHttpActionResult GetPageApi(int id)
        {
            PageApi pageapi = db.PageApis.Find(id);
            if (pageapi == null)
            {
                return NotFound();
            }

            return Ok(pageapi);
        }

        // PUT api/Default1/5
        public IHttpActionResult PutPageApi(int id, PageApi pageapi)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != pageapi.Id)
            {
                return BadRequest();
            }

            db.Entry(pageapi).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PageApiExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST api/Default1
        [ResponseType(typeof(PageApi))]
        public IHttpActionResult PostPageApi(PageApi pageapi)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.PageApis.Add(pageapi);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = pageapi.Id }, pageapi);
        }

        // DELETE api/Default1/5
        [ResponseType(typeof(PageApi))]
        public IHttpActionResult DeletePageApi(int id)
        {
            PageApi pageapi = db.PageApis.Find(id);
            if (pageapi == null)
            {
                return NotFound();
            }

            db.PageApis.Remove(pageapi);
            db.SaveChanges();

            return Ok(pageapi);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool PageApiExists(int id)
        {
            return db.PageApis.Count(e => e.Id == id) > 0;
        }
    }
}